package com.hyyt.shopjsp.store.info.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.pojo.ShopShopInfo;
import com.hyyt.shopjsp.store.info.pojo.ShopShops;
import com.hyyt.shopjsp.store.info.pojo.ShopShopsShopInfo;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.store.info.service.IShopShopsService;
import com.hyyt.shopjsp.store.info.service.IShopShopsShopInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/25 0024下午 17:07.
 */

@Controller
@RequestMapping("/back/shopsShopInfo")
public class ShopShopsShopInfoController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopShopsShopInfoController.class);
	private String basePath = "/back/shop/shopsShopinfo/";
	@Resource
	private IShopShopsService shopShopsService;//线下商场service
	@Resource
	private IShopShopsShopInfoService shopsShopInfoService;//商城与店铺关系service
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺service
	/**
	* 跳转到商城列表页面
	* @params shopsId
	* @params purviewId
	* @params params
	* @return
	*/
	@RequestMapping("gotoShopsShopInfoPage")
	public String gotoShopsShopInfoPage(String shopsId,Map<String, Object> params){
		ShopShops shops = shopShopsService.loadShopShops(Integer.parseInt(shopsId));
		params.put("shops", shops);
		params.put("shopsId", shopsId);
		return basePath+"shopsShopinfoList";
	}

	/**
	* 查询商城与店铺信息
	* @params page
	* @params response
	* @params request
	*/
	@RequestMapping("findShopsShopInfoList")
	@ResponseBody
	public void findShopsShopInfoList(String shopsId,String shopName,Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName.trim());
		}
		params.put("shopsId", shopsId);
		int totalRecordCount =shopsShopInfoService.countShopShopsShopInfo(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<Map<String,Object>> shopsShopInfoListMap =shopsShopInfoService.queryShopsShopInfoMap(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", shopsShopInfoListMap);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	* 删除传入的ids
	*
	* @params ids
	*/
	@RequestMapping(value = "deleteShopsShopInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteShopsShopInfo(String shopsShopinfoId) {
		if (Utils.objectIsNotEmpty(shopsShopinfoId)) {
			shopsShopInfoService.deleteShopShopsShopInfo(Integer.parseInt(shopsShopinfoId));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 查询审核通过的未在商圈中的店铺集合
	 * @param shopsId		商圈ID
	 * @param shopName		店铺名称
	 * @param customerName	店铺管理员名称
	 */
	@RequestMapping("shopInfoNotInShopsList")
	@ResponseBody
	public void shopInfoNotInShopsList(String shopsId,String shopName,String customerName, Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName);
		}
		if(Utils.objectIsNotEmpty(customerName)){
			params.put("customerName", customerName);
		}
		params.put("shopsId", shopsId);
		int totalRecordCount =shopsShopInfoService.countShopInfoNotInShops(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopInfo> shopInfoList =shopsShopInfoService.shopInfoNotInShopsList(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.clear();
		params.put("total",  totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", shopInfoList);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 * 保存线下商城店铺
	 * @param shopsId		商圈ID
	 * @param shopInfoId	店铺ID
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateShopsShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShopsShopInfo(String shopsId,String shopInfoId){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopsId)){
			ShopShops shops=shopShopsService.loadShopShops(Integer.parseInt(shopsId));
			ShopShopInfo shopInfo = shopShopInfoService.loadShopShopInfo(Integer.parseInt(shopInfoId));
			if(Utils.objectIsNotEmpty(shopInfo)){
				ShopShopsShopInfo shopsShopInfo=new ShopShopsShopInfo();
				shopsShopInfo.setShopsId(shops.getShopsId());
				shopsShopInfo.setCity(shops.getCity());
				shopsShopInfo.setRegionLocation(shops.getRegionLocation());
				shopsShopInfo.setShopCategoryId(shopInfo.getShopCategoryId());
				shopsShopInfo.setShopInfoId(shopInfo.getShopInfoId());
				shopsShopInfoService.saveShopShopsShopInfo(shopsShopInfo);
				flag=true;
			}
		}
		return ResultMsg.create(flag);
	}
}
